package com.smart.campus.web.controller.student;

import java.util.List;

import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.Msg;
import com.smart.campus.school.service.IMsgService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author Zhihe.Li
 * @date 2020-06-16
 */
@Controller
@RequestMapping("/student/msg")
public class StudentMsgController extends BaseController
{
    private String prefix = "school/msg";

    @Autowired
    private IMsgService msgService;

    @RequiresPermissions("school:msg:view")
    @GetMapping()
    public String msg()
    {
        return prefix + "/msg";
    }

    /**
     * 查询消息列表
     */
    @RequiresPermissions("school:msg:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Msg msg)
    {
        startPage();
        List<Msg> list = msgService.selectMsgList(msg);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @RequiresPermissions("school:msg:export")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Msg msg)
    {
        List<Msg> list = msgService.selectMsgList(msg);
        ExcelUtil<Msg> util = new ExcelUtil<Msg>(Msg.class);
        return util.exportExcel(list, "msg");
    }

    /**
     * 新增消息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息
     */
    @RequiresPermissions("school:msg:add")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Msg msg)
    {
        return toAjax(msgService.insertMsg(msg));
    }

    /**
     * 修改消息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Msg msg = msgService.selectMsgById(id);
        mmap.put("msg", msg);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @RequiresPermissions("school:msg:edit")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Msg msg)
    {
        return toAjax(msgService.updateMsg(msg));
    }

    /**
     * 删除消息
     */
    @RequiresPermissions("school:msg:remove")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(msgService.deleteMsgByIds(ids));
    }
    /**
     * 消息全部已读
     */
    @PostMapping("/readAll")
    @ResponseBody
    public AjaxResult readALl()
    {
        SysUser user = ShiroUtils.getSysUser();
        Msg msg = new Msg();
        msg.setUserId(user.getUserId());
        msg.setUserType(2);
        msg.setStatus(1);
        return toAjax(msgService.updateMsg(msg));
    }

    /**
     * 消息已读
     */
    @PostMapping( "/read")
    @ResponseBody
    public AjaxResult read(Long id)
    {
        SysUser user = ShiroUtils.getSysUser();
        Msg msg = new Msg();
        msg.setUserId(user.getUserId());
        msg.setUserType(2);
        msg.setId(id);
        msg.setStatus(1);
        return toAjax(msgService.updateMsg(msg));
    }
}
